package sys

import (
	"ginweb-admin/commons/base/service"
	"ginweb-admin/global"
	"ginweb-admin/model/commons/request"
	"ginweb-admin/model/context"
	"ginweb-admin/model/entity/sys"
	"ginweb-admin/model/vo"
)

// 用户结构体
type SysUserService struct {
	service.BaseService[uint, sys.SysUser]
}

// 用户账号登录
func (service *SysUserService) GetUserByAccount(account string) (sysUser *sys.SysUser) {
	// 根据用户名进行查询
	err := global.DB.Unscoped().Where("account = ?", account).First(&sysUser).Error
	if err != nil {
		return nil
	}
	return sysUser
}

// 用户手机号登录
func (service *SysUserService) GetUserByPhone(account string) (sysUser *sys.SysUser) {
	// 根据用户名进行查询
	err := global.DB.Unscoped().Where("phone = ?", account).First(&sysUser).Error
	if err != nil {
		return nil
	}
	return sysUser
}

// 用户昵称登录
func (service *SysUserService) GetUserByUsername(account string) (sysUser *sys.SysUser) {
	// 根据用户名进行查询
	err := global.DB.Unscoped().Where("username = ?", account).First(&sysUser).Error
	if err != nil {
		return nil
	}
	return sysUser
}

// 账号登录、手机号登录、昵称登录
func (service *SysUserService) LoginAccountAndPhoneAndNickname(account string) *sys.SysUser {
	user := service.GetUserByAccount(account)
	if user == nil {
		user = service.GetUserByPhone(account)
		if user == nil {
			user = service.GetUserByUsername(account)
		}
	}
	return user
}

// 添加系统用户
func (service *SysUserService) SaveData(sysUser *sys.SysUser) (err error) {
	err = global.DB.Create(sysUser).Error
	return err
}

// 修改系统用户
func (service *SysUserService) UpdateData(sysUser *sys.SysUser, mapField *map[string]any) (err error) {
	err = global.DB.Unscoped().Model(sysUser).Updates(mapField).Error
	return err
}

// 修改系统用户状态
func (service *SysUserService) UpdateStatus(status context.StatusContext) (err error) {
	sysUser := sys.SysUser{}
	err = global.DB.Model(sysUser).Unscoped().Where("id = ?", status.Id).Update(status.Field, status.Value).Error
	return err
}

// 修改密码
func (service *SysUserService) UpdateSysUserPwd(sysUser *sys.SysUser) (err error) {
	err = global.DB.Model(sysUser).Updates(sysUser).Error
	return err
}

// 根据id删除（软删除）
func (service *SysUserService) DeleteById(id uint) (err error) {
	var sysUser sys.SysUser
	err = global.DB.Where("id = ? ", id).Delete(&sysUser).Error
	return err
}

// 根据id删除（真实删除）
func (service *SysUserService) UnDeleteById(id uint) (err error) {
	var sysUser sys.SysUser
	err = global.DB.Unscoped().Where("id = ? ", id).Delete(&sysUser).Error
	return err
}

// 批量删除系统用户（软删除）
func (service *SysUserService) DeleteByIds(sysUser []*sys.SysUser) (err error) {
	err = global.DB.Delete(&sysUser).Error
	return err
}

// 批量删除系统用户（真删除）
func (service *SysUserService) UnDeleteByIds(sysUser []*sys.SysUser) (err error) {
	err = global.DB.Unscoped().Delete(&sysUser).Error
	return err
}

// 根据id查询系统用户信息
func (service *SysUserService) GetById(id uint) (sysUser *sys.SysUser, err error) {
	err = global.DB.Unscoped().Where("id = ?", id).Find(&sysUser).Error
	return
}

// 分页查询系统用户信息（分页 + 检索条件）
func (service *SysUserService) LoadPage(info request.PageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 查询数据库的表
	db := global.DB.Table("sys_user as t1")

	// 准备切片存放数据
	var sysUserVos []vo.SysUsersVo

	// 查询用户表和中间表的数据（多表联合查询）
	db.Select("t1.*,(SELECT GROUP_CONCAT(role_id) FROM sys_user_role WHERE user_id = t1.id) as RoleIds")

	// 添加检索条件
	if info.Keyword != "" {
		db = db.Where("t1.username like ? or t1.account like ?", "%"+info.Keyword+"%", "%"+info.Keyword+"%")
	}
	// 排序默时间降序降序
	db = db.Order("t1.create_time desc")

	// 查询总数
	// Unscoped 删除和未删除的都会查询出来
	err = db.Unscoped().Count(&total).Error
	if err != nil {
		return sysUserVos, total, err
	} else {
		// 执行查询
		err = db.Unscoped().Limit(limit).Offset(offset).Find(&sysUserVos).Error
	}

	// 结果返回
	return sysUserVos, total, err
}
